home *** CD-ROM | disk | FTP | other *** search
/ Gigarom 4 / Mac Giga-ROM 4.0 - 1993.toast / FILES / BBS / HERMES / ProtMover6.1.cpt / ASCII / read.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-08  |  2.2 KB  |  104 lines  |  [TEXT/KAHL]

  1. /*    ASCII Transfer External for Hermes © 1990 By John Raymonds.
  2.  
  3.     Written in THINK C 4.0.1 as an example external for Hermes protocol
  4.     developers.  Use of any code shown below in any program other than
  5.     an external to be used with Hermes requires written permission from
  6.     the author:
  7.     
  8.     John Raymonds
  9.     76174,205 (Compuserve)
  10.     D3885 (AppleLink)
  11.     
  12.     Comments and suggestions are welcome.
  13.     
  14. */
  15.  
  16. #include <:Mac #includes:SerialDvr.h>
  17.  
  18. #include "Protocol.h"
  19. #include "ASCII.h"
  20.  
  21. extern ProtoRecPtr PRP;
  22. extern ProtoGloPtr PGP;
  23.  
  24. /*    waits seconds seconds for a char on the modem port and returns it */
  25.  
  26. WaitForChar(seconds)
  27. int seconds;
  28. {
  29.     register int theChar;
  30.     register ProtoRecPtr prp;
  31.     register ProtoGloPtr pgp;
  32.     register long timeouttime;
  33.     prp = PRP;
  34.     pgp = PGP;
  35.     FlushWrite();
  36.     timeouttime = TickCount() + seconds * 60;
  37.     for (;;) {
  38.         theChar = modemIn();
  39.         if (prp->F.B.carrierLost) {
  40.             /*    return timeout if transfer stoped */
  41.             return(-1);
  42.         }
  43.         if (theChar >= 0) {
  44.             return(theChar);
  45.         }
  46.         if (TickCount() > timeouttime) {
  47.             /*    return -1 on time out */
  48.             return(-1);
  49.         }
  50.     }
  51. }
  52.  
  53. /*    reads in all bytes that come in within a second of each other */
  54.  
  55. FlushModemInput()
  56. {
  57.     while(WaitForChar(1) != -1) {}
  58. }
  59.  
  60. /*    returns -1 if no characters are available */
  61.  
  62. int modemIn()
  63. {
  64.     long bytesAvail;
  65.     register ProtoRecPtr prp;
  66.     register ProtoGloPtr pgp;
  67.     pgp = PGP;
  68.     if (pgp->rbValid) {
  69.         --pgp->rbValid;
  70.         return((int) pgp->readBuffer[pgp->rbPos++]);
  71.     }
  72.     else {
  73.         SerGetBuf(prp->mRefIn, StripAddress(&bytesAvail));
  74.         if (bytesAvail > READBUFFSIZE) {
  75.             bytesAvail = READBUFFSIZE;
  76.         }
  77.         if (bytesAvail) {
  78.             /*    read in a bunch */
  79.             pgp->MIOPin.ioBuffer = (Ptr) StripAddress(&pgp->readBuffer);
  80.             pgp->MIOPin.ioReqCount = bytesAvail;
  81.             if (!prp->F.B.carrierLost) {
  82.                 PBRead(StripAddress(&pgp->MIOPin), TRUE);
  83.             }
  84.             do {
  85.                 if (prp->F.B.carrierLost) {
  86.                     PBKillIO(StripAddress(&pgp->MIOPin), FALSE);
  87.                     pgp->MIOPin.ioResult = 0;
  88.                     pgp->MIOPin.ioActCount = 0;
  89.                 }
  90.                 Return(prp->timeOut, &pgp->SSR);
  91.             } while(pgp->MIOPin.ioResult == 1);
  92.             pgp->rbValid = pgp->MIOPin.ioActCount;
  93.             pgp->rbPos = 0;
  94.             if (pgp->rbValid) {
  95.                 return(modemIn());
  96.             }
  97.         }
  98.         else {
  99.             Return(prp->timeOut, &pgp->SSR);
  100.         }
  101.     }
  102.     return(-1);
  103. }
  104.